package com.chance.cc.crawler.prod.command.job.domain.news.tianya.tianyakeys;

import com.alibaba.fastjson.JSON;
import com.chance.cc.crawler.core.CrawlerEnum;
import com.chance.cc.crawler.core.CrawlerJob;
import com.chance.cc.crawler.core.downloader.HttpPage;
import com.chance.cc.crawler.core.filter.FilterUtils;
import com.chance.cc.crawler.core.record.CrawlerRequestRecord;
import com.chance.cc.crawler.meta.core.bean.CrawlerMetaConstant;
import com.chance.cc.crawler.meta.core.bean.job.CrawlerScheduleJob;
import com.chance.cc.crawler.prod.command.job.domain.news.NewsCommonScript;
import org.apache.commons.lang3.StringUtils;

public class TianYaNewsCrawlerScheduleJob extends NewsCommonScript {
    public static final String DOMAIN = "tianya";
    public static void main(String[] args) {
        publishTianYaArticleCrawlerScheduleJobInfo();

    }
    public static CrawlerJob publishTianYaArticleCrawlerScheduleJobInfo(){
        CrawlerJob crawlerJob = articleSchedule();

        CrawlerScheduleJob crawlerScheduleJob = new CrawlerScheduleJob();
        crawlerScheduleJob.setDomain(DOMAIN);
        crawlerScheduleJob.setCrawlerJob(JSON.toJSONString(crawlerJob));
        crawlerScheduleJob.setJobType(CrawlerMetaConstant.ScheduleCrawlerJobType.crawler.enumVal());
        crawlerScheduleJob.setNote("天涯关键字采集文章信息");
        crawlerScheduleJob.setCrawlerKey(crawlerJob.generateCrawlerKey());
        HttpPage httpPage = metaServiceCommand.addOrUpdateCrawlerScheduleJob(crawlerScheduleJob);
        System.out.println("发布作业："+httpPage.getRawText());
        return crawlerJob;
    }
    public static CrawlerJob articleSchedule(){
        String site = "product";//public --> private?
        String start_url = "https://search.tianya.cn/bbs?q=nike&pn=1&s=4";
        String site_biz = "realtime";
        CrawlerRequestRecord crawlerRecord = CrawlerRequestRecord.builder()
                .startPageRequest(DOMAIN, CrawlerEnum.CrawlerRequestType.turnPage)
                .domain(DOMAIN)
                .recordKey(start_url)
                .httpUrl(start_url)
                .releaseTime(System.currentTimeMillis())
                .filter(CrawlerEnum.CrawlerRecordFilter.dateRange)
                //.filter(CrawlerEnum.CrawlerRecordFilter.keyOrDateRange)
                //.addFilterInfo(FilterUtils.redisFilterKeyInfo(StringUtils.joinWith("-","filter",domain,site,site_biz,"queue")))
                .addFilterInfo(FilterUtils.dateRangeFilterInfo(24,null))
                .build();
        crawlerRecord.tagsCreator().bizTags().addDomain(DOMAIN);
        crawlerRecord.tagsCreator().bizTags().addSite(site);
        crawlerRecord.tagsCreator().bizTags().addSiteBiz(site_biz);

        String requestQueueName = StringUtils.joinWith("-", "crawler",DOMAIN, site, site_biz, "queue");
        CrawlerJob crawlerJob = CrawlerJob.builder()
                .crawlerJobThreadNumber(3)
                .triggerInfo(
                        DOMAIN,
                        CrawlerMetaConstant.ScheduleJobTrigger_Simple,
                        System.currentTimeMillis(),//tianya-product-realtime-realtime
                        StringUtils.joinWith("-", site, site_biz, CrawlerMetaConstant.ScheduleJobTriggerJob_Realtime))//crawler-tianya-product-realtime-queue
                .crawlerRequestQueue(CrawlerMetaConstant.redisRequestQueue(requestQueueName))
                //.kafkaResultPipeline(null, kafkaTopic, null)
                .fileResultPipeline(null, "/data/chance_crawler_runner/logs/node/tianya.log", false)
                .requestRecord(crawlerRecord)
                .build();
        return crawlerJob;
    }
}
